<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class login extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('login_model');
    }

    public function index() {

        $this->form_validation->set_rules('email', 'E-MAIL', 'required|max_lenght[254]|trim|strtolower|valid_email');
        $this->form_validation->set_rules('encrypted_password', 'SENHA', 'required');

        if ($this->form_validation->run() == TRUE) {
            $dados_login = elements(array('email', 'encrypted_password'), $this->input->post());
            $dados_login['encrypted_password'] = md5($dados_login['encrypted_password']);
            $info = $this->login_model->get_login_data($dados_login['email'], $dados_login['encrypted_password']);

            $this->session->set_userdata($info);

            redirect('main');
        }

        $dados = array(
            //carrega views
            'header' => 'header.php',
            'divheader' => 'divheader_notlogged.php',
            'divcontent' => 'divcontent_login.php',
            'divleftmenu' => 'divleftmenu_notlogged.php',
            'divfooter' => 'divfooter.php',
        );
        $this->load->view('main', $dados);
    }

    public function alterar_senha() {
        
        $this->form_validation->set_rules('encrypted_password', 'SENHA', 'required');
        $this->form_validation->set_rules('nova_encrypted_password', 'NOVA SENHA', 'required');
        $this->form_validation->set_rules('confirma_encrypted_password', 'CONFIRMA NOVA SENHA', 'required|matches[nova_encrypted_password]');
        
        if($this->form_validation->run()==TRUE){
            $senha_atual= md5(element('encrypted_password', $this->input->post()));
            $nova_senha=  md5(element('nova_encrypted_password', $this->input->post()));
            $this->login_model->altera_senha($this->session->userdata['id'],$senha_atual,$nova_senha);
        }
            $dados = array(
            //carrega views
            'header' => 'header.php',
            'divheader' => 'divheader.php',
            'divcontent' => 'divcontent_altera_senha.php',
            'divleftmenu' => 'divleftmenu.php',
            'divfooter' => 'divfooter.php',
            'perfil' => retornaperfil($this->session->userdata('user_role')),
            'menu_item' => criamenu($this->session->userdata('id'),$this->session->userdata('user_role')),
         
        );
        $this->load->view('main', $dados);   
    }
}